package com.leetcode.algorithm.y22.m09.w3;

import java.util.Arrays;

/**
 * 1624. 两个相同字符之间的最长子字符串
 * 
 * https://leetcode.cn/problems/largest-substring-between-two-equal-characters/
 * 
 * @author jie.deng
 *
 */
class Question1624Solution01 {

	public int maxLengthBetweenEqualCharacters(String s) {
		int len = s.length();
		int[] idxArr = new int[26];
		Arrays.fill(idxArr, -1);
		int maxLen = -1;

		for (int i = 0; i < len; i++) {
			int idx = s.charAt(i) - 'a';
			if (idxArr[idx] == -1) {
				idxArr[idx] = i;
			} else {
				if (i - idxArr[idx] - 1 > maxLen) {
					maxLen = i - idxArr[idx] - 1;
				}
			}
		}

		return maxLen;
	}

}